﻿      using System;
using System.Collections.Generic;

using System.Text;
using Xeno.Physics;
using OpenTK;

using OpenTK.Graphics.OpenGL;
namespace Xeno.Core
{
    public class XenoGraph
    {
        public XenoRenderTarget Target { get; set; }
        public XenoTextureDepth DepthBuffer { get; set; }
        public string Name { get; set; }
        public XenoNode RootNode { get; private set; }
        public List<XenoFilter> Filters { get; set; }
        public XenoTexture2D FrameBuffer { get; set; }
        public XenoGraph()
        {
            
            Name = "XenoGraph";
            RootNode = new XenoNode("Scene_Root");
            RootNode.GraphOwner = this;
            RootNode.Active = true;
            ResetRoot();


        }

        public XenoGraph(string name)
        {
        
            Name = name;
            RootNode = new XenoNode(name+"_Scene_Root");
            RootNode.GraphOwner = this;
            ResetRoot();
            Target = new XenoRenderTarget();
        }

        public void ResetRoot()
        {
          // XenoNode node = new XenoNode(Name+"_GraphPhysicsManager",RootNode);
         //   node.AddModule = new XenoPhysicsManager(node.Name);
            Filters = new List<XenoFilter>();
        }
        public XenoFilter AddFilter
        {
            set
            {
                Filters.Add(value);
            }
        }
        public void UpdateGraph()
        {
            RootNode.PerformUpdate();
        }
        public XenoTexture2D RenderGraph()
        {
            if (Filters.Count > 0)
            {

                if (FrameBuffer == null || FrameBuffer.Width!=XenoCore.DisplayWidth || FrameBuffer.Height!=XenoCore.DisplayHeight)
                {
                    FrameBuffer = new XenoTexture2D((int)XenoCore.DisplayWidth, (int)XenoCore.DisplayHeight);
                    DepthBuffer = new XenoTextureDepth((int)XenoCore.DisplayWidth,(int) XenoCore.DisplayHeight);
                }

                Target.Bind();

                Target.AttachTexture(FrameBuffer);
                Target.AttachDepth(DepthBuffer);
             
                RootNode.PerformRender();

                Target.Unbind();
              
                XenoTexture2D prev = FrameBuffer;
                foreach (var fx in Filters)
                {
                    fx.PerformRender(this, prev);
                    prev = fx.Output;
                }

                XuiWidget.Begin2D();

                GL.Color4(System.Drawing.Color.White);

                prev.Bind(0);

                GL.Begin(BeginMode.Quads);

                GL.TexCoord2(0, 1);
                GL.Vertex2(0, 0);
                GL.TexCoord2(1, 1);
                GL.Vertex2(XenoCore.DisplayWidth, 0);
                GL.TexCoord2(1, 0);
                GL.Vertex2(XenoCore.DisplayWidth, XenoCore.DisplayHeight);
                GL.TexCoord2(0, 0);
                GL.Vertex2(0, XenoCore.DisplayHeight);

                GL.End();

                prev.Unbind(0);

                XuiWidget.End2D();
                
                return prev;
            }
            else
            {
         
                RootNode.PerformRender();
          
                return FrameBuffer;
            }
            FrameBuffer = null;
        }

    }
}
